package com.csproj.basic.service;

import com.csproj.basic.BasicService;
import com.csproj.basic.domain.ElectronicLockMessage;
import com.csproj.basic.repository.ElectronicLockMessageRepository;
import com.csproj.basic.repository.SiteRepository;
import com.csproj.basic.service.dto.ElectronicLockMessageDTO;
import com.csproj.basic.service.mapper.ElectronicLockMessageMapper;
import com.csproj.dispatch.domain.Dispatch;
import com.csproj.dispatch.repository.DispatchRepository;
import com.csproj.dispatch.service.WarnInfoService;
import com.csproj.dispatch.service.dto.WarnInfoDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.base.common.exception.BaseAppException;
import com.csproj.system.domain.Dictionary;
import com.csproj.system.service.DictionaryService;
import com.csproj.system.service.UserInfoService;
import com.csproj.system.service.dto.DictionaryDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing ElectronicLockMessage.
 */
@Service
@Transactional
public class ElectronicLockMessageService extends BasicService<ElectronicLockMessageDTO> {

    private final Logger log = LoggerFactory.getLogger(ElectronicLockMessageService.class);

    private final ElectronicLockMessageRepository electronicLockMessageRepository;

    private final ElectronicLockMessageMapper electronicLockMessageMapper;

    private final DispatchRepository dispatchRepository;

    private final WarnInfoService warnInfoService;

    private final DictionaryService dictionaryService;

    public ElectronicLockMessageService(ElectronicLockMessageRepository electronicLockMessageRepository,
                                        ElectronicLockMessageMapper electronicLockMessageMapper, DispatchRepository dispatchRepository, WarnInfoService warnInfoService, DictionaryService dictionaryService) {
        this.electronicLockMessageRepository = electronicLockMessageRepository;
        this.electronicLockMessageMapper = electronicLockMessageMapper;
        this.dispatchRepository = dispatchRepository;
        this.warnInfoService = warnInfoService;
        this.dictionaryService = dictionaryService;
    }


    /**
     * 保存电子关锁信息
     *
     * @param electronicLockMessage
     * @return electronicLockMessage
     * TODO 纯粹的保存方法，通用于所有保存业务调用
     * TODO 赋默认值，保存后，检查数据库，是否所有字段都有准确值
     */
    public ElectronicLockMessage save(ElectronicLockMessage electronicLockMessage) {

        return electronicLockMessageRepository.save(electronicLockMessage);
    }

    /**
     * 获取所有电子关锁信息
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<ElectronicLockMessageDTO> findAll() {
        List<ElectronicLockMessageDTO> result = electronicLockMessageRepository.findAll().stream()
                .map(electronicLockMessageMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
        return result;
    }

    /**
     * 获取所有电子关锁信息（分页）
     *
     * @return the list of entities
     * <p>
     * TODO 返回纯粹的数据库对象，不包含扩展属性，适用于所有调取业务方法
     */
    @Transactional(readOnly = true)
    public Page<ElectronicLockMessage> findPageList(Map<String, Object> map, Pageable pageable) {
        Page<ElectronicLockMessage> result = electronicLockMessageRepository.findPageList(map, pageable);
        return result;
    }

    /**
     * 根据id，获取电子关锁信息
     *
     * @param id 主键id
     * @return creator、questionTypeName
     * <p>
     * TODO 返回DTO，包含扩展属性
     * TODO 如果扩展属性不同，findPageListAndGetDTO1、findPageListAndGetDTO2，注明扩展的属性
     */
    @Transactional(readOnly = true)
    public ElectronicLockMessageDTO findOneAndGetDTO(String id) {
        ElectronicLockMessage entity = findOne(id);
        ElectronicLockMessageDTO dto = electronicLockMessageMapper.toDto(entity);

        return dto;
    }

    /**
     * 根据id，获取电子关锁信息
     *
     * @param id the id of the entity
     * @return the entity
     * <p>
     * TODO 返回纯粹的数据库对象，不包含扩展属性，适用于所有调取业务方法
     */
    @Transactional(readOnly = true)
    public ElectronicLockMessage findOne(String id) {
        ElectronicLockMessage entity = electronicLockMessageRepository.findOne(id);
        if (entity == null) {
            throw new BaseAppException(BaseDTO.CODE_PARAM, "id_errors");
        }
        return entity;
    }

    public ElectronicLockMessageDTO findFirstByElNumberOrderByTimeStringDesc(String lockNo) {
        ElectronicLockMessage entity = electronicLockMessageRepository.findFirstByElNumberOrderByTimeDesc(lockNo);
        return electronicLockMessageMapper.toDto(entity);
    }
}
